package com.wx.mythread.stopthread;

public class StopSleep implements Runnable{

    private Object lock = new Object();

    private int i = 0;


    @Override
    public void run() {
        while (true){
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "还在运行");
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Interrupted");
                    break;
                }
                // mywait();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void mywait(){
        try {
            //Thread.sleep(2000);
            synchronized (lock){
                lock.wait();
            }
        } catch (InterruptedException e) {
            //interrupted();
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        StopSleep stopSleep = new StopSleep();
        Thread thread = new Thread(stopSleep);
        thread.start();
        Thread.sleep(50);
        thread.interrupt();
    }
}
